package cn.iocoder.yudao.module.member.service.companytemplate;

import cn.iocoder.yudao.module.member.controller.admin.companytemplate.vo.CompanyTemplateAuditReqVo;
import cn.iocoder.yudao.module.member.controller.app.companytemplate.vo.CompanyTemplatePageReqVO;
import cn.iocoder.yudao.module.member.controller.app.companytemplate.vo.CompanyTemplateSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.member.dal.dataobject.companytemplate.CompanyTemplateDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.companytemplate.CompanyTemplateMapper;

import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 会员公司信息模板 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CompanyTemplateServiceImpl implements CompanyTemplateService {

    @Resource
    private CompanyTemplateMapper companyTemplateMapper;

    @Override
    public Long createCompanyTemplate(CompanyTemplateSaveReqVO createReqVO) {
        // 插入
        CompanyTemplateDO companyTemplate = BeanUtils.toBean(createReqVO, CompanyTemplateDO.class);
        companyTemplateMapper.insert(companyTemplate);
        // 返回
        return companyTemplate.getId();
    }

    @Override
    public void updateCompanyTemplate(CompanyTemplateSaveReqVO updateReqVO) {
        // 校验存在
        validateCompanyTemplateExists(updateReqVO.getId());
        // 更新
        CompanyTemplateDO updateObj = BeanUtils.toBean(updateReqVO, CompanyTemplateDO.class);
        companyTemplateMapper.updateById(updateObj);
    }

    @Override
    public void auditCompanyTemplate(CompanyTemplateAuditReqVo updateReqVO) {
        // 校验存在
        CompanyTemplateDO templateDO = companyTemplateMapper.selectById(updateReqVO.getId());
        if (templateDO == null) {
            throw exception(COMPANY_TEMPLATE_NOT_EXISTS);
        }
        if (!Objects.equals(templateDO.getStatus(), CompanyTemplateDO.STATUS_AUDITING)) {
            throw exception(COMPANY_TEMPLATE_STATUS_NOT_AUDIT);
        }
        // 更新
        BeanUtils.copyProperties(updateReqVO, templateDO);
        companyTemplateMapper.updateById(templateDO);
    }

    @Override
    public void deleteCompanyTemplate(Long id) {
        // 校验存在
        validateCompanyTemplateExists(id);
        // 删除
        companyTemplateMapper.deleteById(id);
    }

    private void validateCompanyTemplateExists(Long id) {
        if (companyTemplateMapper.selectById(id) == null) {
            throw exception(COMPANY_TEMPLATE_NOT_EXISTS);
        }
    }

    @Override
    public CompanyTemplateDO getCompanyTemplate(Long id) {
        return companyTemplateMapper.selectById(id);
    }

    @Override
    public PageResult<CompanyTemplateDO> getCompanyTemplatePage(CompanyTemplatePageReqVO pageReqVO) {
        return companyTemplateMapper.selectPage(pageReqVO);
    }

}